package utils;

import java.util.LinkedList;
import java.util.Queue;

public class CreateNode {
    public void createTree(Queue<Integer> tree, TreeNode node) {
        //深搜创建
//        Integer val = tree.remove();
//        if (val != null){
//            TreeNode left = new TreeNode(val);
//            node.left = left;
//            createTree(tree, left);
//        }
//        val = tree.remove();
//        if (val != null){
//            TreeNode right = new TreeNode(val);
//            node.right = right;
//            createTree(tree, right);
//        }

        //广搜创建
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(node);
        while (!queue.isEmpty()) {
            node = queue.remove();
            if (tree.isEmpty()) break;
            Integer val = tree.remove();
            if (val != null) {
                TreeNode left = new TreeNode(val);
                node.left = left;
                queue.offer(left);
            }
            val = tree.remove();
            if (val != null) {
                TreeNode right = new TreeNode(val);
                node.right = right;
                queue.offer(right);
            }
        }
    }
}
